const express = require("express");
const router = express.Router();
const diarybookModel = require("../model/diarybook.js");
const diaryItemModel = require("../model/diaryItems.js");
const replyModel = require("../model/reply.js");
const { send, sendErr } = require("./send");

router.get('/', async function (req, res) { // 获取获取日记
    try {
        let { diaryItemId, diarybookId } = req.query;
        if (diaryItemId) {
            let diaryItem = await diaryItemModel.findById(diaryItemId);
            if (diaryItem) send(res, { data: { diaryItem } });
            else sendErr(res, { msg: "没有该日记" });
        } else {
            let diarybook = await diarybookModel.findById(diarybookId);
            let time = new Date();
            time.setHours(0, 0, 0);
            if (diarybook.overduetime > time){
                let data = await diaryItemModel.find({ diarybookId, createAt: { $gt: time } });
                send(res, { data });
            } else {
                let data = await diaryItemModel.find({diarybookId});
                send(res , {data});
            }
        }
    } catch (error) {
        console.log(error);
        sendErr(res, { msg:"传入的数据可能有问题"});
    }
})

router.get("/homepage" , async function(req , res){ // 首页获取日记
    try{
        let { limit, offset } = req.query;
        let time = new Date();
        time.setHours(0, 0, 0);
        let diaryItems = await diaryItemModel.find({ createAt: { $gt: time } });
        diaryItems.reverse();
        let data = diaryItems.slice(offset, limit + offset);
        if (limit + offset >= diaryItems.length) data.noMore = true;
        send(res, { data });
    }catch(err){
        console.log(err);
        sendErr(res , {msg :"传入的数据可能有问题"});
    }
})

router.post("/create" , async function(req , res){ // 创建日记
    try {
        let { diarybookId, createAt, content, importent } = req.body;
        let diarybook = await diarybookModel.findById(diarybookId);
        if (diarybook) {
            let diaryItem = await diaryItemModel.create({
                authorId: diarybook.authorId,
                diarybookId,
                createAt,
                content,
                importent
            })
            if (diaryItem) {
                await diarybookModel.findByIdAndUpdate(diarybookId, { $push: { diaryItems: diaryItem._id } });
                send(res, { data: { diaryItem } });
            } else sendErr(res, { msg: "创建失败" });
        } else sendErr(res, { msg: "日记本不存在" });
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})

router.post("/revise", async function (req, res) { // 修改日记
    try {
        let { diaryItemId, diarybookId, content, importent } = req.body;
        let diaryItem = await diaryItemModel.findById(diaryItemId);
        let obj = {};
        if (content) obj.content = content;
        if (importent !== undefined) obj.importent = importent;

        if (diaryItem) {
            if (diarybookId) {
                await diarybookModel.findByIdAndUpdate(diaryItem.diarybookId, { $pull: { diaryItems: diaryItemId } });
                await diarybookModel.findByIdAndUpdate(diarybookId, { $push: { diaryItems: diaryItemId } });
                await diaryItemModel.findByIdAndUpdate(diaryItemId, { diarybookId });
            }
            await diaryItemModel.findByIdAndUpdate(diaryItemId, { ...obj });
            send(res);
        } else {
            sendErr(res, { msg: "日记不存在" });
        }
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})

router.delete("/:id" , async function(req , res){ // 删除日记
    try {
        let { id } = req.params;
        let diaryItem = await diaryItemModel.findById(id);
        if (diaryItem) {
            await diarybookModel.findByIdAndUpdate(diaryItem.diarybookId, { $pull: { diaryItems: id } });
            for (let item of diaryItem.replyId) {
                await replyModel.findByIdAndDelete(item);
            }
            await diaryItemModel.findByIdAndDelete(id);
            send(res);
        } else sendErr(res, { msg: '日记不存在' });
    } catch (error) {
        console.log(error);
        sendErr(res, { msg: "传入的数据可能有问题" });
    }
})

module.exports = router;